This example shows how to read View coordinates in different Coordinate spaces.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Top")
.frame(width: 200, height: 200)
.background(Color.blue)
.coordinateSpace(name: "Custom")
GeometryReader { geo in
Text("Hello, World!")
.frame(width: geo.size.width * 0.9, height: geo.size.height * 0.5)
.background(Color.red)
.onTapGesture {
let global = geo.frame(in: .global)
let local = geo.frame(in: .local)
let custom = geo.frame(in: .named("Custom"))
print("GLOBAL ----")
print("origin = \(global.origin)")
print("min = \(global.minX), \(global.minY)")
print("mid = \(global.midY), \(global.midY)")
print("max = \(global.maxX), \(global.maxY)")
print("size = \(global.size)")
print("wh = \(global.width), \(global.height)")
print("LOCAL ----")
print("origin = \(local.origin)")
print("min = \(local.minX), \(local.minY)")
print("mid = \(local.midY), \(local.midY)")
print("max = \(local.maxX), \(local.maxY)")
print("size = \(local.size)")
print("wh = \(local.width), \(local.height)")
print("CUSTOM ----")
print("origin = \(custom.origin)")
print("min = \(custom.minX), \(custom.minY)")
print("mid = \(custom.midY), \(custom.midY)")
print("max = \(custom.maxX), \(custom.maxY)")
print("size = \(custom.size)")
print("wh = \(custom.width), \(custom.height)")
}
}.border(Color.green, width: 5)
}
}
}
Coordinate spaces